package cc.lj.internal.coupons.domain.ticket.service.impl;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.model.vo.CouponsSettlementDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketMainDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.convert.TicketMainPOConvertDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketBatchRepository;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketMainRepository;
import cc.lj.internal.coupons.domain.ticket.service.ITicketBatchDomainService;
import cc.lj.internal.coupons.domain.ticket.service.ITicketMainDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchLifecycleContinuePO;
import cc.lj.internal.coupons.infrastructure.po.TicketMainPO;
import cc.lj.internal.coupons.infrastructure.repository.TicketBatchLifecycleContinueRespository;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 电子券主表领域服务service实现类
 * @author liangxifeng
 * @date 2023-06-14
 */
@Service("ticketMainDomainServiceImpl")
@Slf4j
public class TicketMainDomainServiceImpl implements ITicketMainDomainService {
    //电子券批次表仓储接口
    @Resource(name = "ticketMainRepository")
    private ITicketMainRepository repository;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageBean<TicketMainDO> selectPage(PageParam<TicketMainDO> pageParam) {
        PageInfo<TicketMainPO> pageInfo = repository.selectPage(pageConvert(pageParam));

        List<TicketMainDO> list = new ArrayList();
        if(pageInfo.getList().size() > 0)
        {
            for (TicketMainPO ticketMainPO : pageInfo.getList()) {
                TicketMainDO activityDO = TicketMainPOConvertDO.INSTANCE.convert(ticketMainPO);
                list.add(activityDO);
            }
        }
        //list的po转换为do后再组装PageBean将其返回上一层
        PageBean pageBean = PageBean.createPageBean(pageInfo.getPageNum(),
                pageInfo.getPageSize(), pageInfo.getTotal(), list);
        return pageBean;
    }


    /**
     * 分页DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<TicketMainPO> pageConvert(PageParam<TicketMainDO> pageParam) {
        PageParam<TicketMainPO> pageParamR = new PageParam<TicketMainPO>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        TicketMainPO activityPO = TicketMainPOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(activityPO);
        return pageParamR;
    }

    /**
     * 通过主键查询一条记录
     * @param id
     * @return
     */
    public TicketMainDO getById(Integer id)
    {
        TicketMainPO ticketMainPO = repository.getById(id);
        return TicketMainPOConvertDO.INSTANCE.convert(ticketMainPO);
    }

    /**
     * 根据id修改
     * @param ticketMainDO
     * @return
     */
    public Integer updateById(TicketMainDO ticketMainDO) {
        //TicketMainPO mainPO = repository.getById(ticketMainDO.getId());
        TicketMainPO ticketMainPO = TicketMainPOConvertDO.INSTANCE.convert(ticketMainDO);
        return repository.updateById(ticketMainPO);
    }

    /**
     * 标准批量获取数据的方法
     * @param ticketMainDO
     * @return
     */
    public List<TicketMainDO> getList(TicketMainDO ticketMainDO)
    {
        TicketMainPO TicketMainPO = TicketMainPOConvertDO.INSTANCE.convert(ticketMainDO);
        List<TicketMainPO> list = repository.getList(TicketMainPO);
        List<TicketMainDO> newList = new ArrayList<>();
        for(TicketMainPO item : list)
        {
            newList.add(TicketMainPOConvertDO.INSTANCE.convert(item));
        }
        return newList;
    }

    @Override
    public List<TicketMainDO> getByIds(List<Integer> idList) {
        return repository.selectIds(idList).stream()
                .map(ticketMainPO->TicketMainPOConvertDO.INSTANCE.convert(ticketMainPO))
                .collect(Collectors.toList());
    }

    @Override
    public Integer updateByIds(List<Integer> idList, TicketMainDO ticketMainDO) {
        return repository.updateByIds(idList,TicketMainPOConvertDO.INSTANCE.convert(ticketMainDO));
    }
}
